package org.ziegler.log.analysis.core;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.math.NumberUtils;

public class SplitColumn {
    private final String key;
    private final String str;
    private final LocalDateTime dateTime;
    private final int serverId;
    private final String fileName;

    private SplitColumn(String str, String key, LocalDateTime dateTime, int serverId, String fileName) {
        this.key = key;
        this.str = str;
        this.dateTime = dateTime;
        this.serverId = serverId;
        this.fileName = fileName;
    }

    public String getKey() {
        return key;
    }

    public String getStr() {
        return str;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public int getServerId() {
        return serverId;
    }

    public String getFileName() {
        return fileName;
    }

    public static class Builder {
        private Builder() {
        }

        public static SplitColumn parse(String str, String fileName) {

            // 解析行号
            final String key = getFileNumKey(str);

            // 解析日期
            final LocalDateTime dateTime = getDateTime(str, fileName);

            // 解析serverId
            int serverId = getServerId(str);

            return new SplitColumn(str, key, dateTime, serverId, fileName);
        }

        public static int getServerId(String str) {
            final int endIndex = str.indexOf("区");

            if (endIndex != -1) {
                String strServerId = str.substring(0, endIndex);
                int serverId = NumberUtils.toInt(strServerId);
                return serverId;
            }

            return 0;
        }

        public static LocalDateTime getDateTime(String str, String fileName) {
            final int beginIndex = str.indexOf("@@@");
            final int midIndex = str.indexOf(" ");
            final int endIndex = str.indexOf(",");

            LocalDateTime dateTime = LocalDateTime.MIN;

            if (beginIndex != -1 && endIndex != -1
                    && beginIndex < endIndex
                    && midIndex != -1) {
                String strDataTime = str.substring(beginIndex+3, endIndex+4);
                final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");

                try {
                    dateTime = LocalDateTime.parse(strDataTime, formatter);
                } catch (Exception e) {
                    //System.out.println("日志文件[" + fileName + "]解析时间数据异常 = " + str);
//                e.printStackTrace();
                    // 作为错误的日期类型，让他显示到最后，防止忽略掉
                    dateTime = LocalDateTime.MAX;
                }
            }

            return dateTime;
        }

        public static String getFileNumKey(String str) {
            final int leftParenthesesIndex = str.indexOf("(");
            final int colonIndex = str.indexOf(":");
            final int rightParenthesesIndex = str.indexOf(")");

            // 解析日志文件行号
            final String key;
            if (leftParenthesesIndex != -1 && rightParenthesesIndex != -1
                    && leftParenthesesIndex < rightParenthesesIndex
                    && colonIndex != -1) {
                key = str.substring(leftParenthesesIndex + 1, rightParenthesesIndex);
            } else {
                // 处理未检测出的数据
                key = "";
            }
            return key;
        }

    }
}
